/**
 *	流程模块主添加单位数据
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-22
 */

package com.framework.Flow;




import com.framerock.baselib.CDate;
import com.framerock.baselib.Rock;
import com.framerock.db.DBWhere;
import com.framework.ACore.Model;
import com.framework.Model.ModelOption;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class FlowUnit extends FlowBase {

    private ModelOption option;


    public Model unitModel;

    public FlowUnit(Flow flows) {
        super(flows);
        option      = ModelOption.get(flows.nDb);
        unitModel   = Model.get(this.nDb, "flowunit");
    }

    /**
     * 添加系统模块时
     * */
    public void flowUnitadd()
    {
        Model tfobj = Model.get(this.nDb, "flowtodo");
        ArrayList<Map<String, String>> rows = tfobj.getAll("`modeid`="+mFlow.modeid+" AND `cid`=0 AND `status`=1");
        Map<String, String> da;
        String num,where;
        String cid = mRequest.COMPANYID;

        if(rows != null)for(int i=0; i<rows.size(); i++){
            da  = rows.get(i);
            num = da.get("num");
            if(Rock.isEmpt(num))num = "bh"+da.get("id")+"";
            where = "`cid`="+cid+" AND `modeid`="+mFlow.modeid+" AND `num`='"+num+"'";
            da.put("num", num);
            da.put("cid", cid);
            da.remove("id");

            if(tfobj.getCount(where)==0) {
                tfobj.insertbo(da);
            }else{
                tfobj.update(da, where);
            }
        }

        Model mfobj = Model.get(this.nDb, "flowmenu");
        rows = mfobj.getAll("`modeid`="+mFlow.modeid+" AND `cid`=0 AND `status`=1");
        if(rows != null)for(int i=0; i<rows.size(); i++){
            da  = rows.get(i);
            num = da.get("num");
            if(Rock.isEmpt(num))num = "bh"+da.get("id")+"";
            where = "`cid`="+cid+" AND `modeid`="+mFlow.modeid+" AND `num`='"+num+"'";
            da.put("num", num);
            da.put("cid", cid);
            da.remove("id");

            if(mfobj.getCount(where)==0) {
                mfobj.insertbo(da);
            }else{
                mfobj.update(da, where);
            }
        }
    }


    private Map<String, String> pidAar= new HashMap<String, String>();

    /**
     * 获取数据选项的id
     * */
    public int getOptionpid(String num, String name, int pid)
    {
        String key = ""+num+""+pid+"";
        int id = Rock.getMapint(pidAar, key);
        if(id > 0)return id;
        String where = "`pid`="+pid+" AND `num`='"+num+"'";
        if(Rock.isEmpt(num) && !Rock.isEmpt(name))where = "`pid`="+pid+" AND `name`='"+name+"'";
        Map<String,String> mrs = option.getOne(where);
        if(mrs==null){
            id = option.insert(Rock.getMap(new String[]{
                    "name", name,
                    "num", num,
                    "pid", ""+pid,
                    "optdt", CDate.now()
            }));
        }else{
            id = Rock.getMapint(mrs, "id");
        }
        pidAar.put(key, id+"");
        return id;
    }

    /**
     * 添加数据选项
     * @param pname 上级名称如：行政选项-通知公告类型
     * @param num 编号
     * @param downcont 下级数组
     * */
    public void addOption(String pname, String num, String downcont[])
    {
        int pid = getOptionpid("xinhu_"+mRequest.COMPANYID+"", mRequest.getCompanyrs("name"), 0);
        int id  = getAllpid(pname, pid, ""+num+"_"+mRequest.COMPANYID+"");
        Map<String,String> da;
        String name,where;
        for(int i=0;i<downcont.length;i++){
            da   = Rock.getMap(downcont[i]);
            name = da.get("name");
            where= "`pid`="+id+" and `name`='"+name+"'";
            da.put("pid", ""+id);
            if(option.getOne(where)==null){
                da.put("optdt", CDate.now());
                option.insertbo(da);
            }
        }
    }

    /**
     * 获取对应上级id
     * */
    public int getAllpid(String pname, int pid, String num)
    {
        String arr[] = pname.split("-");
        String nums  = "";
        int len = arr.length;
        for(int i=0;i<len;i++){
            nums= "";
            if(i+1 == len)nums = num;
            pid = getOptionpid(nums, arr[i], pid);
        }
        return pid;
    }

    /**
     * 判断是否管理员
     * */
    public Boolean isManage(String aid)
    {
        Boolean bo = false;
        if(!mFlow.UnitModeBo)return bo;
        String manageid = Rock.getMapString(mFlow.unitrs, "manageid");
        if(!Rock.isEmpt(manageid) && Rock.contain(","+manageid+",", ","+aid+","))bo = true;
        return bo;
    }

    /**
     * 更新单位模块信息，第一次新增才更新
     * @param da 更新Map信息
     * @param unid 模块模块id
     * @param url 更新地址，默认新增是url是空的，或者
     * */
    public void updateUnit(Map<String, String> da, String unid, String url)
    {
        //String nurl = Rock.getMapString(mFlow.unitrs, "url");
        String where= DBWhere.start()
                .winto("cid", mFlow.COMPANYID)
                .wint("modeid", mFlow.modeid)
                .wstr("url", url)
                .show();
        int to = unitModel.getCount(where);
        if(to == 1) unitModel.update(da,unid);
    }
}
